#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <cstdio>
#include <cstdlib>

using namespace std;

void manipulate(vector< vector<int> > &,string &);
void InitializePosition(vector< vector<int> > &, int, int);
string get_blocks(vector<int> &);
int stoi(char *);

int main()
{
	int block_number;
	string command;

	cin >> block_number;
	vector< vector<int> > block_array(block_number);
	for(int i=0; i < block_array.size(); ++i)
		block_array[i].push_back(i);

	cin.ignore();
	while( getline(cin, command, '\n') )
	{
		if( command.find("quit") != string::npos )
			break;
		else
			manipulate(block_array, command);
	}
	for(int i=0; i < block_array.size(); ++i)
	{
		cout << i << ":" << get_blocks(block_array[i]) << endl;
	}

#ifndef ONLINE_JUDGE
	system("pause");
#endif

	return 0;
}

void manipulate(vector< vector<int> > &block_array, string &cmd)
{
	//check a equal b or in which a & b are in the same block
	int a, b, posa = -1, posb = -1;
	vector<int>::iterator ita, itb;
	char number1[3], number2[3];

	cmd.copy(number1,3,5);
	a = stoi(number1);
	if( a < 10)
		cmd.copy(number2,3,11);
	else
		cmd.copy(number2,3,12);
	b = stoi(number2);

	if(a == b)
		return;

	for(int i=0; i < block_array.size(); ++i)
	{
		if(!block_array[i].empty())
			for(vector<int>::iterator it = block_array[i].begin(); it != block_array[i].end(); ++it)
			{
				if(*it == a)
				{
					posa = i;
					ita = it;
				}
				if(*it == b)
					posb = i;
				if( (posa != -1) && (posb != -1) && (posa == posb) )
					return;
			}
	}
	if( cmd.find("move") != string::npos )
		InitializePosition(block_array, posa, a);
	if( cmd.find("onto") != string::npos )
		InitializePosition(block_array, posb, b);
	//a assign b
	block_array[posb].insert(block_array[posb].end(), ita, block_array[posa].end());
	block_array[posa].erase(ita, block_array[posa].end());
}
void InitializePosition(vector< vector<int> > &block_array, int pos, int val)
{
	for(vector<int>::reverse_iterator rit = block_array[pos].rbegin(); rit != block_array[pos].rend(); ++rit)
	{
		int back_val = *rit;
		if( back_val == val )
			break;
		block_array[back_val].push_back(back_val);
		block_array[pos].pop_back();
	}
}
string get_blocks(vector<int> &block_vector)
{
	stringstream nums;
	if( !block_vector.empty() )
	{
		for(vector<int>::iterator it = block_vector.begin(); it != block_vector.end(); ++it)
			nums << ' ' << *it;
	}
	return nums.str();
}
int stoi(char * s)
{
	int i;

	sscanf(s, "%d", &i);

	return i;
}
